#include "CTcpTestClient.h"

//////////////////////////////////////////////////////////////////
//  CSelClient
//////////////////////////////////////////////////////////////////

CSelClient::CSelClient()
{
}

CSelClient::~CSelClient()
{
}

BOOL CSelClient::RunClient()
{
    if (!isConnect()) return FALSE;

    fd_set fdsRead = { 0 };
    timeval tv = { 1, 0 };

    FD_ZERO(&fdsRead);
    FD_SET(GetSocket(), &fdsRead);

    select((int)GetSocket() + 1, &fdsRead, NULL, NULL, &tv);

    if (FD_ISSET(GetSocket(), &fdsRead))
    {
        FD_CLR(GetSocket(), &fdsRead);

        if (SOCKET_HANDLE != OnHandleMsg())
        {
            Close();
            return FALSE;
        }
    }

    return TRUE;
}


//////////////////////////////////////////////////////////////////
//  CClient
//////////////////////////////////////////////////////////////////

SOCKET_STATUS CClient::MsgProc(IN NET_PACKET_HEADER* lpHeader)
{
    static size_t i = 0;
    NET_COMMON_PACKET packet;

    switch (lpHeader->nCmd)
    {
    case NORMALACK:
        //printf("%lld-%s\r\n", ++i, ((NET_COMMON_PACKET*)lpHeader)->strInfo);
        //printf("<socket>%lld</socket>\t<Length>%d</Length\t><id>%lld</id>\r\n", ClientSocket, lpHeader->nDataLength, i++);
        //send(GetSocket(), (const char*)&packet, sizeof(NET_COMMON_PACKET), 0);
        strcpy(packet.strInfo, "Windows 11");
        Send(&packet);
        break;
    case LOGIN:
        break;
    case USERON:
        //printf("<socket>%lld</socket>\t<Length>%d</Length>\t<id>%lld</id>\r\n", ClientSocket, lpHeader->nDataLength, i++);
        break;
    case USEROFF:
        //printf("<socket>%lld</socket>\t<Length>%d</Length>\t<id>%lld</id>\r\n", ClientSocket, lpHeader->nDataLength, i++);
        break;
    case DISCONNECT:
        return SOCKET_CLOSE;
        break;
    default:
        printf("Error\r\n");
        break;
    }

    return SOCKET_HANDLE;
}
